Login/ Register With Facebook

Course- PHP Tutorial >

Now a days most of the project required login through social media. This article will guide you to crate login/ register with Facebook.

Step1:

Please insert the Following script inside body tag of your login/register page.

<div id="fb-root" style="float:left; width:1px;"></div>

<script>
window.fbAsyncInit = function() {
    FB.init({
       appId: 'Enter your API here',
       cookie: true,
       xfbml: true,
       oauth: true
    }); 
};


(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());

function fblogin(){
FB.login(function(response){
if (response.authResponse) {
window.location='validatefb.php';
}
},{scope: 'publish_stream'});
}
</script>


Click here to know more about Permissions in Facebook.

Click here to generate App ID/API Key and App Secret for Facebook.

Step 2:
Please insert login with face book button and write down the following code.

<img border="0" src="images/login_facebook.png" onClick="fblogin();" border="0" 
style="cursor:pointer">

The above code will open a popup to login with Facebook.

Login with Facebook

Step3:

validatefb.php  will check if the user has already register with your system or not. If the user registered before then the system will take the user to welcome page or it will take the user to register page.

File Included in this projects are:

connection.php: This is the simple database connection file. You can use your own database connection file.

facebook_constants.php: This is the file you need to change and use your own API key and secret key.
Click here to know how to generate API key and Secret id .